/**
 * Created by Administrator on 2018/1/19.
 */
myApp_.onPageInit('choose_place', function (page) {

});

CHOICE_COMMUNITY = {};
CHOICE_COMMUNITY.ad_code = 0;
CHOICE_COMMUNITY.init = function (code) {
    mainView.router.load({
        url: "src/index/choice_community/choice_community.html",
    });
    CHOICE_COMMUNITY.location(function (e) {
        CHOICE_COMMUNITY.request();
    });
}
CHOICE_COMMUNITY.oninput = function () {
    CHOICE_COMMUNITY.request();
}


CHOICE_COMMUNITY.request = function () {
    var search = $("#my_addr").val();
    request_(
        url_("api/V1/search_community_list"),
        {
            search: search,
            county_id: CHOICE_COMMUNITY.ad_code,
        }, function (d) {
            if (d.code == 0) {
                var community_list = d.data;
                CHOICE_COMMUNITY.community_list(community_list);
            } else {
                my_tips_(d.msg);
            }
        }
    );
}


CHOICE_COMMUNITY.save_ = function (id, name) {
    id = SESSION.set_community_id(id);
    SESSION.set_community_name(name);
    if (id) {
        go_("");
    }
}
CHOICE_COMMUNITY.community_list = function (community_list) {
    var tpl = '  <li class="item-content"  onclick="CHOICE_COMMUNITY.save_(%s,\'%s\')">' +
        '<div  class="item-inner">' +
        '   <span style="font-weight: 700;font-size: .9rem;color: #333;">%s</span>' +
        '<br/>' +
        '   <span style="color: #333;font-size: .32rem;">%s</span>' +
        '</div>' +
        '</li>';
    var html = '';
    for (i in community_list) {
        html += sprintf(tpl, community_list[i].id, community_list[i].community_name, community_list[i].community_name, community_list[i].community_address);
    }
    $("#choice_community_list").html(html);
}
CHOICE_COMMUNITY.location = function (callback) {
    SESSION.location(function (e) {
        CHOICE_COMMUNITY.ad_code = e.ad_code;
        callback(e);
        setTimeout(function () {
            $("#choice_community_location_show").html(
                sprintf("%s/%s/%s", e.province, e.city, e.district)
            );
        }, 3 * 100)
    });
}